package com.loong.mapper.impl;

import com.loong.mapper.UserMapper;
import com.loong.pojo.User;
import com.loong.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

/**
 * 数据库访问实现类
 */
public class UserMapperImpl implements UserMapper {
    Logger logger = Logger.getLogger(UserMapperImpl.class);
    @Override
    public User selectUserByUsername(String username) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUserByUsername(username);
        }catch (Exception e){
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public int insertUser(User user) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.insertUser(user);
            sqlSession.commit();
            return i;
        }catch (Exception e){
            logger.error(e.getMessage());
            return 0;
        }
    }

    @Override
    public User selectUserByCode(String code) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUserByCode(code);
        }catch (Exception e){
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public int updateUserStatusByUid(int uid) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.updateUserStatusByUid(uid);
            sqlSession.commit();
            return i;
        }catch (Exception e){
            logger.error(e.getMessage());
            return 0;
        }
    }

}
